package in.muton.edushare.pages.cp.courseFBCP;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.panels.IPollPanelContainer;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.PollsDAO;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Universities;

public class PollImportPanel extends EdusharePanel {
	private static final long serialVersionUID = -8725757026256168986L;
	IModel<Universities> univModel = null;

	private IModel<Poll> selectedPoll = null;
	private IModel<Poll> newPoll = null;

	public PollImportPanel(String id, IModel<Universities> univModel,
			final IPollPanelContainer container, IModel<Poll> currentPoll) {
		super(id);
		this.univModel = univModel;
		this.newPoll = currentPoll;
		Form<Void> f = new Form<Void>("pollSelectionForm") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				if (selectedPoll != null) {
					PollsDAO pDAO = new PollsDAO(getORMInterface());
					pDAO.copyPollQuestions(newPoll.getObject(), selectedPoll
							.getObject());
					container.gotoNextPage();
				}
			}
		};
		add(f);
		f.add(new DropDownChoice<IModel<Poll>>(
				"pollSelection",
				new Model<IModel<Poll>>() {
					private static final long serialVersionUID = 2613640953254922446L;

					@Override
					public void setObject(IModel<Poll> m) {
						selectedPoll = m;
					}

					@Override
					public IModel<Poll> getObject() {
						return selectedPoll;
					}
				}, ListCoursePolls.initializeListOfPolls(getLoggedInUser(),
						getORMInterface(), univModel),
				new IChoiceRenderer<IModel<Poll>>() {
					private static final long serialVersionUID = 1L;

					public Object getDisplayValue(IModel<Poll> arg0) {
						return arg0.getObject().getPollName();
					}

					public String getIdValue(IModel<Poll> arg0, int arg1) {
						return arg0.getObject().getPollId().toString();
					}
				}));
	}
}
